unit ufuncionario;

interface

uses
  Windows, variants, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  DBCtrls, StdCtrls, Mask, ExtCtrls, Db, DBTables, Buttons, Grids, DBGrids,
  ComCtrls, QuickRpt, ToolWin, ActnList, DBClient, QRCtrls;

type
  Tffuncionario = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    Panel2: TPanel;
    ScrollBox1: TScrollBox;
    ScrollBox2: TScrollBox;
    Label31: TLabel;
    Label30: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label5: TLabel;
    Label4: TLabel;
    Label9: TLabel;
    Label14: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label17: TLabel;
    Label7: TLabel;
    Label10: TLabel;
    Label18: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    EditData: TDBEdit;
    EditCodigo: TDBEdit;
    EditNomeFntasia: TDBEdit;
    EditNome: TDBEdit;
    EditEndereco: TDBEdit;
    EditBairro: TDBEdit;
    EditCidade: TDBEdit;
    EditCEP: TDBEdit;
    EditCGC: TDBEdit;
    EditIE: TDBEdit;
    EditFone: TDBEdit;
    DBEdit3: TDBEdit;
    EditFoneFax: TDBEdit;
    EditContato: TDBEdit;
    EditCargoContato: TDBEdit;
    DBEdit9: TDBEdit;
    ScrollBox3: TScrollBox;
    DBGrid3: TDBGrid;
    Panel4: TPanel;
    Label26: TLabel;
    Edit2: TEdit;
    Panel1: TPanel;
    ToolBar2: TToolBar;
    btnPrimeiro: TSpeedButton;
    btnVoltar: TSpeedButton;
    btnAvancar: TSpeedButton;
    btnUltimo: TSpeedButton;
    Panel6: TPanel;
    Panel7: TPanel;
    btnnovo: TSpeedButton;
    Panel5: TPanel;
    btngravar: TSpeedButton;
    Panel8: TPanel;
    btncancelar: TSpeedButton;
    Panel12: TPanel;
    SpeedButton21: TSpeedButton;
    Panel11: TPanel;
    Panel3: TPanel;
    btnimprimir: TSpeedButton;
    Panel9: TPanel;
    Panel14: TPanel;
    dsfuncionario: TDataSource;
    ActionList1: TActionList;
    novo: TAction;
    voltar: TAction;
    avancar: TAction;
    foco: TAction;
    filtro: TAction;
    formatar: TAction;
    Panel10: TPanel;
    DBGrid1: TDBGrid;
    Button1: TButton;
    SpeedButton1: TSpeedButton;
    Label1: TLabel;
    Bevel1: TBevel;
    DBGrid2: TDBGrid;
    Label8: TLabel;
    dsitem: TDataSource;
    Label6: TLabel;
    Label11: TLabel;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    QuickRep1: TQuickRep;
    QRBand1: TQRBand;
    LControle1: TQRLabel;
    QRLabel2: TQRLabel;
    QRLabel3: TQRLabel;
    QRLabel4: TQRLabel;
    QRLabel5: TQRLabel;
    QRLabel6: TQRLabel;
    QRSysData2: TQRSysData;
    QRBand2: TQRBand;
    QRDBText1: TQRDBText;
    QRDBText2: TQRDBText;
    QRDBText3: TQRDBText;
    QRDBText4: TQRDBText;
    QRDBText5: TQRDBText;
    tbfuncionario: TTable;
    tbitem: TTable;
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure Label3Click(Sender: TObject);
    procedure Label4Click(Sender: TObject);
    procedure Label5Click(Sender: TObject);
    procedure SpeedButton12Click(Sender: TObject);
    procedure Edit2Change(Sender: TObject);
    procedure QRCompositeReport5AddReports(Sender: TObject);
    procedure btngravarClick(Sender: TObject);
    procedure btnnovoClick(Sender: TObject);
    procedure novoExecute(Sender: TObject);
    procedure btnPrimeiroClick(Sender: TObject);
    procedure btnVoltarClick(Sender: TObject);
    procedure btnAvancarClick(Sender: TObject);
    procedure btnUltimoClick(Sender: TObject);
    procedure btncancelarClick(Sender: TObject);
    procedure SpeedButton21Click(Sender: TObject);
    procedure tbclienteAfterOpen(DataSet: TDataSet);
    procedure tbclienteBeforeEdit(DataSet: TDataSet);
    procedure voltarExecute(Sender: TObject);
    procedure avancarExecute(Sender: TObject);
    procedure focoExecute(Sender: TObject);
    procedure EditDataKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure tbclientePostError(DataSet: TDataSet; E: EDatabaseError;
      var Action: TDataAction);
    procedure EditNomeFntasiaExit(Sender: TObject);
    procedure btnimprimirClick(Sender: TObject);
    procedure EditCodigoKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure EditNomeFntasiaKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure PageControl1Change(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure DBGrid2Enter(Sender: TObject);
    procedure DBGrid2Exit(Sender: TObject);
    procedure DBGrid2KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure Label7Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Edit2Exit(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure Button4Click(Sender: TObject);
    procedure Table1AfterEdit(DataSet: TDataSet);
    procedure Table1AfterPost(DataSet: TDataSet);
    procedure Table1AfterScroll(DataSet: TDataSet);
    procedure Table2AfterEdit(DataSet: TDataSet);
    procedure Table2NewRecord(DataSet: TDataSet);
    procedure formatarExecute(Sender: TObject);
    procedure DBGrid2ColExit(Sender: TObject);
  private
    FReport : TQuickRep;
    procedure SetReport(Value : TQuickRep);
  public
  i:integer;
  ST:string;
  bok : boolean;
    property Report : TQuickRep read FReport write SetReport;
   { Private declarations }


  { Public declarations }
  end;

var
  ffuncionario: Tffuncionario;
 Texto, Texto1 : String;
implementation

{$R *.DFM}

procedure Tffuncionario.SetReport(Value : TQuickRep);
begin
  FReport:=Value;
end;

procedure Tffuncionario.FormKeyPress(Sender: TObject; var Key: Char);
begin
 if key=#13 then begin
  key := #0;
       Perform(WM_nextdlgctl,0,0);
 end Else if Key = #27 then
   Perform(WM_nextdlgctl,1,0);
end;

procedure Tffuncionario.Label3Click(Sender: TObject);

begin

        BOK := inputquery('Localizar código','Entre com o código:',ST);
         if BOK then
          if not tbfuncionario.Locate ('Codigo',ST,[]) then
           ShowMessage ('Código não cadastrado !!!');
            ST := '';

end;

procedure Tffuncionario.Label4Click(Sender: TObject);

Var
       Razao:String;
        LocCodogo : Boolean;
begin


     LocCodogo := InputQuery ('localizar', 'Entre com a Razão Social',Razao );
      if LocCodogo then begin
        if not tbfuncionario.FindKey ([Razao]) then
         ShowMessage ('Nome não encontrado');
          end;
end;

procedure Tffuncionario.Label5Click(Sender: TObject);
begin
        BOK := inputquery('Localizar nome ','Entre com o nome :',ST);
         if BOK then
          if not tbfuncionario.Locate ('NomeFantasia',uppercase(ST),[]) then
           ShowMessage ('Nome  não encontrado !!!');
            ST := '';

end;

procedure Tffuncionario.SpeedButton12Click(Sender: TObject);
begin
        tbfuncionario.Filtered := False;
end;

procedure Tffuncionario.Edit2Change(Sender: TObject);
begin
        tbfuncionario.indexfieldnames := 'nomefantasia';
         tbfuncionario.FindNearest([edit2.text]);
end;

procedure Tffuncionario.QRCompositeReport5AddReports(Sender: TObject);
    begin;
end;

procedure Tffuncionario.btngravarClick(Sender: TObject);
begin
        tbfuncionario.edit;
         tbfuncionario.post;

           btngravar.Enabled:=false;
            btncancelar.Enabled:=false;

end;

procedure Tffuncionario.btnnovoClick(Sender: TObject);

begin
     novoexecute(self);
end;




procedure Tffuncionario.novoExecute(Sender: TObject);
var
it:integer;
begin

     if dbgrid2.Focused = false then begin

     try
      tbfuncionario.DisableControls;
    try
    tbfuncionario.last;

    if tbfuncionario['codigo']<> null then
    it := tbfuncionario['codigo']
    else
    it:= 0;

        tbfuncionario.insert;

         tbfuncionario['codigo'] := it + 1;
         tbfuncionario['admissao'] := datetostr(now);

    finally

    tbfuncionario.EnableControls;
    end;
    finally

    end;

        btnGravar.Enabled:=true;
         btncancelar.Enabled := true;

            editcodigo.setfocus;
   end else

   tbitem.Insert;

end;

procedure Tffuncionario.btnPrimeiroClick(Sender: TObject);
begin
        tbfuncionario.first;
end;

procedure Tffuncionario.btnVoltarClick(Sender: TObject);
begin
        tbfuncionario.prior;
end;

procedure Tffuncionario.btnAvancarClick(Sender: TObject);
begin
        tbfuncionario.next;
end;

procedure Tffuncionario.btnUltimoClick(Sender: TObject);
begin
                tbfuncionario.last;
end;

procedure Tffuncionario.btncancelarClick(Sender: TObject);
begin
        tbfuncionario.cancel;
          btnGravar.Enabled:=false;
           btncancelar.enabled := false;
end;

procedure Tffuncionario.SpeedButton21Click(Sender: TObject);
begin

          if Application.MessageBox('Deseja excluir este FUNCIONÁRIO? ',
         'Confirme', 4)= idYes then begin

         tbitem.Last;

         while not tbitem.Bof do begin
          tbitem.delete;
           tbitem.Next;
            end;

        tbfuncionario.delete;
          btnGravar.Enabled:=true;
           btncancelar.enabled := true;
        end;

end;

procedure Tffuncionario.tbclienteAfterOpen(DataSet: TDataSet);
begin
        btngravar.enabled := false;
        btncancelar.enabled := false;
end;

procedure Tffuncionario.tbclienteBeforeEdit(DataSet: TDataSet);
begin
       btnGravar.Enabled:=True;
       btncancelar.enabled := true;
end;

procedure Tffuncionario.voltarExecute(Sender: TObject);
begin
        tbfuncionario.prior;
end;

procedure Tffuncionario.avancarExecute(Sender: TObject);
begin
        tbfuncionario.next;
end;

procedure Tffuncionario.focoExecute(Sender: TObject);
begin
        i := i + 1;

        if i >= 4 then i:=1;

        case i of

        1: editdata.setfocus;
        2: dbgrid2.setfocus;
        3: edit2.setfocus;
        end;


end;

procedure Tffuncionario.EditDataKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
        if key = 27 then dbgrid3.setfocus;
end;

procedure Tffuncionario.tbclientePostError(DataSet: TDataSet;
  E: EDatabaseError; var Action: TDataAction);
begin
        Messagedlg('Este cliente já está cadastrado, verificar !!!',mterror,[mbok],0);
         tbfuncionario.cancel;
          abort;
end;

procedure Tffuncionario.EditNomeFntasiaExit(Sender: TObject);
begin
        if length(EditNomeFntasia.text) < 1 then begin
         showmessage('preencha o nome!!!');
           end;
end;

procedure Tffuncionario.btnimprimirClick(Sender: TObject);
begin

         quickrep1.PreviewModal;
end;

procedure Tffuncionario.EditCodigoKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
        if key = vk_f1 then
         label3click(self);
end;

procedure Tffuncionario.EditNomeFntasiaKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
        if key = vk_f1 then
         label5click(self);
end;

procedure Tffuncionario.PageControl1Change(Sender: TObject);
begin
        if pagecontrol1.ActivePageIndex = 0 then
         tbfuncionario.Filtered := false;
end;

procedure Tffuncionario.Button1Click(Sender: TObject);
var
st : string;

begin
        if inputquery('Filtrar Bairro','Entre com o nome do Bairro:',st) then begin

        tbfuncionario.Filtered := false;
         tbfuncionario.Filter:= 'bairro='''+ st + '''';
          tbfuncionario.Filtered := true;
           end;

end;

procedure Tffuncionario.SpeedButton1Click(Sender: TObject);
begin
           quickrep1.PreviewModal;

end;

procedure Tffuncionario.DBGrid2Enter(Sender: TObject);
begin
        btngravarclick(self);
        keypreview :=false;
end;

procedure Tffuncionario.DBGrid2Exit(Sender: TObject);
begin
        keypreview := true;
end;

procedure Tffuncionario.DBGrid2KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
        if key = 13 then
         dbgrid2.SelectedIndex :=  dbgrid2.SelectedIndex + 1;

        if key = 27 then
         dbgrid2.SelectedIndex :=  dbgrid2.SelectedIndex   -1;

end;

procedure Tffuncionario.Label7Click(Sender: TObject);
begin
        BOK := inputquery('Localizar RG','Entre com o RG:',ST);
         if BOK then
          if not tbfuncionario.Locate ('rg',ST,[]) then
           ShowMessage ('RG não localizado !!!');
            ST := '';
end;

procedure Tffuncionario.Button2Click(Sender: TObject);
var
st : string;

begin
        if inputquery('Filtrar CEP','Entre com o Cep:',st) then begin

        tbfuncionario.Filtered := false;
         tbfuncionario.Filter:= 'cep='''+ st + '''';
          tbfuncionario.Filtered := true;
           end;

end;

procedure Tffuncionario.Button3Click(Sender: TObject);
var
st, st1 : string;

begin
        if inputquery('Filtrar Data','Entre com a data inicial:',st) then begin

        if inputquery('Filtrar Data','Entre com a data final:',st1) then begin

        tbfuncionario.Filtered := false;
         tbfuncionario.Filter:= 'admissao>='''+ st + ''' and admissao<='''+ st1 + '''';
          tbfuncionario.Filtered := true;
           end;
            end;

end;

procedure Tffuncionario.Edit2Exit(Sender: TObject);
begin
        tbfuncionario.indexfieldnames := 'chave';
end;

procedure Tffuncionario.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin

        IF btngravar.Enabled = true then begin
         case application.MessageBox('Deseja salvar as alterações?', 'Sair de Cadastro de Cliente',mb_yesnocancel + MB_ICONQUESTION )of

        idyes : begin
         tbfuncionario.edit;
          tbfuncionario.post;
           canclose:=true;
            end;

         idno : begin
          tbfuncionario.cancel;
           canclose:=true;
            end;

         idcancel :
           canclose:=false;
end;
end;


end;

procedure Tffuncionario.Button4Click(Sender: TObject);
begin
        tbfuncionario.Filtered := false;
end;

procedure Tffuncionario.Table1AfterEdit(DataSet: TDataSet);
begin
        btnGravar.Enabled:=true;
         btncancelar.Enabled := true;
end;

procedure Tffuncionario.Table1AfterPost(DataSet: TDataSet);
begin
        btngravar.enabled := false;
        btncancelar.enabled := false;
end;

procedure Tffuncionario.Table1AfterScroll(DataSet: TDataSet);
begin
           btnAvancar.Enabled := True;
            btnUltimo.Enabled := True;
             btnPrimeiro.Enabled := True;
              btnVoltar.Enabled := True;

        if tbfuncionario.Bof then begin
         btnPrimeiro.Enabled := False;
          btnVoltar.Enabled := False;

             end;

        if tbfuncionario.Eof then begin

           btnAvancar.Enabled := False;
            btnUltimo.Enabled := False;
             end;

end;

procedure Tffuncionario.Table2AfterEdit(DataSet: TDataSet);
begin
        btnGravar.Enabled:=true;
         btncancelar.Enabled := true;
end;

procedure Tffuncionario.Table2NewRecord(DataSet: TDataSet);
begin
        tbitem['salario'] := tbfuncionario['salario'];
end;

procedure Tffuncionario.formatarExecute(Sender: TObject);
begin
   (tbfuncionario.fieldbyname ('data') as tdatetimefield).editmask := '99/99/9999';
   (tbfuncionario.fieldbyname ('codigo') as tfloatfield).displayformat := '000000';
   (tbfuncionario.fieldbyname ('fone') as tstringfield).editmask := '(99)9999-9999';
   (tbfuncionario.fieldbyname ('cep') as tstringfield).editmask := '99.999-999';   
   (tbfuncionario.fieldbyname ('Salario') as tfloatfield).displayformat := '###,##0.00';
   (tbfuncionario.fieldbyname ('HoraEntrada') as tdatetimefield).editmask := '99:99';
   (tbfuncionario.fieldbyname ('Horasaida') as tdatetimefield).editmask := '99:99';
   (tbfuncionario.fieldbyname ('Demissao') as tdatetimefield).editmask := '99/99/9999';
   (tbitem.fieldbyname ('mes_ano') as tdatetimefield).editmask := '99/99/9999';
   (tbitem.fieldbyname ('salario') as tfloatfield).displayformat := '###,##0.00';
   (tbitem.fieldbyname ('extra') as tfloatfield).displayformat := '###,##0.00';
   (tbitem.fieldbyname ('desconto') as tfloatfield).displayformat := '###,##0.00';
   (tbitem.fieldbyname ('total') as tfloatfield).displayformat := '###,##0.00';
end;
                
procedure Tffuncionario.DBGrid2ColExit(Sender: TObject);
begin
       tbitem.Edit;
       if dbgrid2.SelectedIndex <> 0 then
        tbitem.FieldByName('total').asfloat := tbitem.FieldByName('salario').asfloat +
         tbitem.FieldByName('extra').asfloat - tbitem.FieldByName('desconto').asfloat;

end;

end.
